#!/usr/bin/perl

#====  PROJECT =================================================================
#         NAME:  Basic Infection
#      PURPOSE:  Test Case for a simple infection network
#      RETURNS:  True
#===============================================================================


# Initialisation: Basic Infection Network.

use warnings;
use insj_m_network;
#use Math::Random::MT::Perl qw(rand);

#Create the network.
	$network = insj_m_network->new(name => "Basic Infection Network");

	for($counter = 1; $counter <= 50; $counter++){
		$node = $network->create_node( name=> $counter);}

#Connect the network and set nodes to uninfected.

	@nodes = $network -> get_node_keys();
	$network -> rewind;

	while ($node = $network -> get_next() ) {

		$node -> set_state("VirusA", 0,0);$node -> set_state("VirusA_t", 0,0);
		$node -> set_state("VirusB", 0,0);$node -> set_state("VirusB_t", 0,0);

		#Picking a random node key and connecting it.
		for($counter = 1; $counter <= 3; $counter++){		
			$rnode = $nodes[rand @nodes];	
			$node -> add_node( $network -> get_node( $rnode ) );
			}
		}

#Infect a single node.

	$rnode = $nodes[rand @nodes];
	$node  = $network -> get_node( $rnode );
	$node -> set_state("VirusA", 1,1); $node -> set_state("VirusA_t", 1,1);

#Print network.
	$network->print;

#Evolve network, transmission probability 10%.
	for($generation = 2; $generation <=100; $generation++){
		print "-------------Generation $generation-----------\n";
		$network->rewind;
		
		while ( $node = $network -> get_next ){
			
			$node->rewind;

			while ( $edge = $node -> get_next ){
				#The Ruleset.
				if ( $edge -> get_state("VirusA") == 1 ) {
					if ( rand(100) <= 10 and $node->get_state("VirusA") == 0) { $node->set_state("VirusA_t", 1, $generation); 						
						}
					}
				}
			}
		#Temporay states to actual states!
		$network->rewind;
		
		while ( $node = $network -> get_next ){
			if ( $node->get_state("VirusA") != $node->get_state("VirusA_t") ) { 
				$node->set_state("VirusA", $node->get_state("VirusA_t"), $generation);  
				print $node->get_name." got infected.\n";}			
			}

		$network->print;
	}

$network->export("test_basic_infection.gexf", "gephi");

1;
